/*
 * Created on Mar 30, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.scohen.juploadr.uploadapi.flickrrest.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.scohen.juploadr.Messages;
import org.scohen.juploadr.app.tags.TagParser;
import org.scohen.juploadr.app.tags.TagsHelper;
import org.scohen.juploadr.prefs.BaseConfigurationForm;
import org.scohen.juploadr.prefs.Fault;
import org.scohen.juploadr.uploadapi.Account;


/**
 * @author steve
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class MetadataForm extends BaseConfigurationForm {
    // private Label privacyLabel;
    private Label tagsLabel;
    private Group privacyComposite;
    // private Group tagsGroup;
    private Button radioPublic;
    private Button radioPrivate;
    private Button friends;
    private Button family;
    private Composite privacyOptions;
    private Text defaultTags;
   
    /**
     * @param arg0
     * @param arg1
     */
    public MetadataForm(Composite parent) {
        super(parent);
        addDisposeListener(new DisposeListener() {
            public void widgetDisposed(DisposeEvent event) {
                MetadataForm.this.disposed(event);
            }
        });

    }

    /**
     * 
     */
    protected void initUI() {
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        this.setLayout(layout);

        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        SelectionListener radioSelectionListener = new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				MetadataForm.this.radioClicked(e);
			}
		};

        privacyComposite = new Group(this, SWT.SHADOW_NONE);
        layout = new GridLayout();
        layout.numColumns = 2;
        privacyComposite.setLayout(layout);
        privacyComposite.setText(Messages
                .getString("juploadr.ui.dialog.label.default.privacy")); //$NON-NLS-1$
        privacyComposite.setLayoutData(data);
        // privacyComposite.setLayout(new FillLayout());
        radioPrivate = new Button(privacyComposite, SWT.RADIO);
        radioPrivate.setText(Messages.getString("juploadr.ui.label.private")); //$NON-NLS-1$
        radioPrivate.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        radioPrivate.addSelectionListener(radioSelectionListener);

        radioPublic = new Button(privacyComposite, SWT.RADIO);
        radioPublic.setText(Messages.getString("juploadr.ui.label.public")); //$NON-NLS-1$
        radioPublic.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        radioPublic.addSelectionListener(radioSelectionListener);

        privacyOptions = new Composite(privacyComposite, SWT.SHADOW_ETCHED_IN);
        privacyOptions.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        privacyOptions.setLayout(new FillLayout());
        friends = new Button(privacyOptions, SWT.CHECK);
        friends.setText(Messages.getString("juploadr.ui.label.friends")); //$NON-NLS-1$
        family = new Button(privacyOptions, SWT.CHECK);
        family.setText(Messages.getString("juploadr.ui.label.family")); //$NON-NLS-1$
        
        Account currentAccount = api.getCurrentAccount();
        if ((Account.PRIVACY_PRIVATE & currentAccount.getDefaultPrivacySetting()) != 0) {
            radioPrivate.setSelection(true);
        } else {
            radioPublic.setSelection(true);
            friends.setEnabled(false);
            family.setEnabled(false);
        }

        if ((currentAccount.getDefaultPrivacySetting() & Account.PRIVACY_FAMILY) != 0) {
            family.setSelection(true);
        }

        if ((currentAccount.getDefaultPrivacySetting() & Account.PRIVACY_FRIENDS) != 0) {
            friends.setSelection(true);
        }

        tagsLabel = new Label(this, SWT.NONE);
        tagsLabel.setText(Messages
                .getString("juploadr.ui.dialog.label.default.tags")); //$NON-NLS-1$

        layout = new GridLayout();
        layout.numColumns = 2;
        defaultTags = new Text(this, SWT.MULTI | SWT.WRAP | SWT.BORDER);
        defaultTags.setText(TagParser.toInputString(currentAccount.getDefaultTags()));
        data = new GridData(GridData.FILL_BOTH);
        defaultTags.setLayoutData(data);
        defaultTags.addKeyListener(new TagsHelper());
    }

    private void disposed(DisposeEvent event) {
        radioPrivate.dispose();
        radioPublic.dispose();
        privacyComposite.dispose();
        tagsLabel.dispose();
        defaultTags.dispose();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.prefs.ConfigurationForm#saveChanges()
     */
    public void saveChanges() {
        Account account = api.getCurrentAccount();
        account.setDefaultTags(TagParser.parse(defaultTags.getText()));
        int privacySetting = 0;

        if (friends.getSelection()) {
            privacySetting |= Account.PRIVACY_FRIENDS;
        }

        if (family.getSelection()) {
            privacySetting |= Account.PRIVACY_FAMILY;
        }

        if (radioPrivate.getSelection()) {
            privacySetting |= Account.PRIVACY_PRIVATE;
        } else {
            privacySetting = Account.PRIVACY_PUBLIC;
        }

        account.setDefaultPrivacySetting(privacySetting);
        account.save();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.prefs.ConfigurationForm#validateChanges()
     */
    public Fault validateChanges() {
       
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.prefs.ConfigurationForm#getFormName()
     */
    public String getFormName() {
        // TODO Auto-generated method stub
        return Messages.getString("juploadr.ui.dialog.metadataform.title"); //$NON-NLS-1$
    }

    private void radioClicked(SelectionEvent event) {
        friends.setEnabled(radioPrivate == event.widget);
        family.setEnabled(radioPrivate == event.widget);

    }
    
    public void accountChanged() {
        resetUI();
        initUI();
        this.layout();
    }
}